package com.qf.anli.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.qf.anli.pojos.User;
import com.qf.anli.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/login")
    public boolean login(@RequestBody User user, HttpServletRequest req){
        //通过service，访问dao，执行数据库查询

        Wrapper<User> qw = Wrappers.lambdaQuery(User.class)
                .eq(User::getUsername,user.getUsername())
                .eq(User::getPassword,user.getPassword());

        User loginUser = userService.getOne(qw);

        if (loginUser != null){
            //登录成功 ① 将用户信息存入session ② 返回
            req.getSession().setAttribute("user",loginUser);

            return true;
        }

        return false;
    }
}
